package com.housekeeping.housekeeping.cleanerInformation.service.impl;

import com.housekeeping.housekeeping.cleanerInformation.dao.ICleanerDao;
import com.housekeeping.housekeeping.cleanerInformation.dao.impl.CleanerDaoImpl;
import com.housekeeping.housekeeping.cleanerInformation.entity.Cleaner;
import com.housekeeping.housekeeping.cleanerInformation.proxy.DaoProxy;
import com.housekeeping.housekeeping.cleanerInformation.service.ICleanerService;
import com.housekeeping.housekeeping.cleanerInformation.utils.Page;

import java.sql.SQLException;
import java.util.List;

/**
 * @author hua
 * @version JDK1.8
 * @date 2021/1/19 9:57
 */
public class CleanerServiceImpl implements ICleanerService {
    private ICleanerDao cleanerDao;

    /**
     *
     */
    public CleanerServiceImpl(){
        DaoProxy daoProxy = new DaoProxy();
        cleanerDao = (ICleanerDao) daoProxy.newProxyInstance(new CleanerDaoImpl());
    }

    /**
     * 添加保洁员信息
     * @param cleaner 保洁员对象
     * @return 数据库操作受影响行数
     * @throws SQLException
     */
    @Override
    public int cleanerInsert(Cleaner cleaner) throws SQLException {
        int i = cleanerDao.insert(cleaner);
        return i;
    }

    /**
     *
     * @param page 当前页
     * @param size 一页中的数据条数
     * @return
     * @throws SQLException
     */
    @Override
    public Page<Cleaner> list(int page, int size) throws SQLException {
        List<Cleaner> cleaners = cleanerDao.selectByPage((page - 1) * size, size);
        //count 数据库总数据的总条数
        int count  = cleanerDao.selectCount();
        Page<Cleaner> pageDean = new Page<>(page, size, count);
        pageDean.setData(cleaners);
        return pageDean;
    }

    /**
     * 模糊查询
     * @param page
     * @param size
     * @param name 查询条件
     * @return
     * @throws SQLException
     */
    @Override
    public Page<Cleaner> searchList(int page, int size, String name) throws SQLException {
        List<Cleaner> cleaners = cleanerDao.vagueSelectName((page - 1) * size, size,name);
        //count 数据库总数据的总条数
        int count  = cleanerDao.selectCount();
        Page<Cleaner> pageDean = new Page<>(page, size, count);
        pageDean.setData(cleaners);
        return pageDean;
    }

    /**
     * 精确查询, 根据电话号码精确查询
     * @param page
     * @param size
     * @param telephoneNumber 电话号码
     * @return
     */
    @Override
    public Page<Cleaner> exactSelectTelephoneNumber(int page, int size, String telephoneNumber) throws SQLException {
        List<Cleaner> cleaners = cleanerDao.selectTelephoneNumber((page - 1) * size, size,telephoneNumber);
        //count 数据库总数据的总条数
        int count  = cleanerDao.selectCount();
        Page<Cleaner> pageDean = new Page<>(page, size, count);
        pageDean.setData(cleaners);
        return pageDean;
    }

    /**
     * 修改保洁员信息
     * @param cleaner 保洁员对象
     * @return 操作数据库返回的受影响行数
     */
    @Override
    public int cleanerUpdate(Cleaner cleaner) throws SQLException {
        int i= cleanerDao.update(cleaner);
        return i;
    }

    /**
     *
     * @param id
     * @return
     * @throws SQLException
     */
    @Override
    public int cleanerDelete(int id) throws SQLException {
        int i = cleanerDao.delete(id);
        return i;
    }

    /**
     *
     * @param list 存放保洁员id的集合
     * @return
     * @throws SQLException
     */
    @Override
    public int cleanerDeleteAll(List<Cleaner> list) throws SQLException {
        int i = cleanerDao.deleteAll(list);
        return i;
    }
}
